package com.quanshu.shop.main.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.view.GoodsInfoActivity;
import com.quanshu.shop.main.entity.ShopMainEntity;

import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/7/18
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class LikeGoodsAdapter extends DelegateAdapter.Adapter<LikeGoodsAdapter.LikeGoodsAdapterViewHolder> {
    private Context mContext;
    private List<ShopMainEntity.LikesEntity> likesEntityList;
    private LayoutHelper layoutHelper;
    public static final int LIKE_GOODS_TYPE = 8;

    public LikeGoodsAdapter(Context mContext, List<ShopMainEntity.LikesEntity> likesEntityList, LayoutHelper layoutHelper) {
        this.mContext = mContext;
        this.likesEntityList = likesEntityList;
        this.layoutHelper = layoutHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @NonNull
    @Override
    public LikeGoodsAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == LIKE_GOODS_TYPE) {
            return new LikeGoodsAdapterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_shop_main_like, parent, false));

        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull LikeGoodsAdapterViewHolder holder, int position) {
        Glide.with(mContext).load(Config.URL_HOST_SHOP + likesEntityList.get(position).getImg()).into(holder.imageView);
        holder.textView1.setText(likesEntityList.get(position).getName());
        holder.textView2.setText("¥ " + likesEntityList.get(position).getSell_price());
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (MyApplication.getInstance().getCurrentUser()==null){
//                    BaseTool.goActivity(mContext, LoginActivity.class,null);
//                    return;
//                }
                Bundle bundle = new Bundle();
                bundle.putString("goods_info_id", likesEntityList.get(position).getId());
                BaseTool.goActivity(mContext, GoodsInfoActivity.class, bundle);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return LIKE_GOODS_TYPE;
    }

    @Override
    public int getItemCount() {
        return likesEntityList != null ? likesEntityList.size() : 0;
    }

    class LikeGoodsAdapterViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        TextView textView1;
        TextView textView2;
        LinearLayout linearLayout;

        public LikeGoodsAdapterViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.shop_main_like_img);
            textView = itemView.findViewById(R.id.shop_main_like_txt);
            textView1 = itemView.findViewById(R.id.shop_main_like_txt1);
            textView2 = itemView.findViewById(R.id.shop_main_like_txt2);
            linearLayout = itemView.findViewById(R.id.shop_main_like_ll);
        }
    }
}
